package tool

import cats.data.Validated
import implicits.Implicits._
import tool.Pojo.PredictData

/**
 * Created by yz on 23/6/2021
 */
class PredictDataValidTool(data:PredictData) {

  def validBmi = {
    val v=data.bmi
    val info="体质指数"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=500
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validFbg = {
    val v=data.fbg
    val info="空腹血糖"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=100
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validGgt = {
    val v=data.ggt
    val info="γ-谷氨酰基转移酶"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=2000
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validHdlch = {
    val v=data.hdlch
    val info="高密度脂蛋白胆固醇"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=100
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validTbil = {
    val v=data.tbil
    val info="总胆红素"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=500
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validAlb = {
    val v=data.albumin
    val info="白蛋白"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=1000
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validAst = {
    val v=data.ast
    val info="天门冬氨酸氨基转移酶"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=1000
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validPlt = {
    val v=data.plt
    val info="血小板计数"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！").andThen{b=>
      Validated.cond(v.isDouble, true, s"${info}必须为实数！")
    }.andThen{b=>
      val min=0
      val max=1000
      Validated.cond(v.toDouble>=min && v.toDouble<=max, true, s"${info}必须处于${min}到${max}之间！")
    }.andThen{b=>
      Validated.cond(v.matches("^\\d*(\\.\\d{1,3})?$"), true, s"${info}最多包含三位小数！")
    }
  }

  def validAge = {
    val age=data.age
    Validated.cond(age.nonBlank, true, s"年龄不能为空！").andThen{b=>
      Validated.cond(age.isInt, true, s"年龄必须为整数！")
    }.andThen{b=>
      Validated.cond(age.toInt>=18 && age.toInt<=100, true, s"年龄必须处于18到100之间！")
    }
  }

  def validSampleId = {
    val v=data.sampleId
    val info="样本号"
    Validated.cond(v.nonBlank, true, s"${info}不能为空！")
  }

}

object PredictDataValidTool{

  def valid(data: PredictData) = {
    val fileValidTool = new PredictDataValidTool(data)
    import fileValidTool._
    validAge.andThen{b=>
      validSampleId
    }.andThen{b=>
      validBmi
    }.andThen{b=>
      validFbg
    }.andThen{b=>
      validGgt
    }.andThen{b=>
      validHdlch
    }.andThen{b=>
      validTbil
    }.andThen{b=>
      validAlb
    }.andThen{b=>
      validAst
    }.andThen{b=>
      validPlt
    }
  }

}
